home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / MacPerl 506 appl folder.sit / MacPerl 506 appl folder / Mac_Perl_506r1m_appl / lib / find.pl < prev    next >
Text File  |  1995-03-20  |  3KB  |  119 lines

  1. # Usage:
  2. #    require "find.pl";
  3. #
  4. #    &find('/foo','/bar');
  5. #
  6. #    sub wanted { ... }
  7. #        where wanted does whatever you want.  $dir contains the
  8. #        current directory name, and $_ the current filename within
  9. #        that directory.  $name contains "$dir/$_".  You are cd'ed
  10. #        to $dir when the function is called.  The function may
  11. #        set $prune to prune the tree.
  12. #
  13. # This library is primarily for find2perl, which, when fed
  14. #
  15. #   find2perl / -name .nfs¥* -mtime +7 -exec rm -f {} ¥; -o -fstype nfs -prune
  16. #
  17. # spits out something like this
  18. #
  19. #    sub wanted {
  20. #        /^¥.nfs.*$/ &&
  21. #        (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
  22. #        int(-M _) > 7 &&
  23. #        unlink($_)
  24. #        ||
  25. #        ($nlink || (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_))) &&
  26. #        $dev < 0 &&
  27. #        ($prune = 1);
  28. #    }
  29. #
  30. # Set the variable $dont_use_nlink if you're using AFS, since AFS cheats.
  31.  
  32. $dont_use_nlink = 1;    # The Mac cheats too
  33.  
  34. sub find {
  35.     chop($cwd = `pwd`);
  36.     foreach $topdir (@_) {
  37.     (($topdev,$topino,$topmode,$topnlink) = stat($topdir))
  38.       || (warn("Can't stat $topdir: $!¥n"), next);
  39.     if (-d _) {
  40.         if (chdir($topdir)) {
  41. #        ($dir,$_) = ($topdir,'.');
  42.         ($dir,$_) = ($topdir,':');
  43.         $name = $topdir;
  44.         &wanted;
  45. #        ($fixtopdir = $topdir) =~ s,/$,, ;
  46.         ($fixtopdir = $topdir) =~ s,:$,, ;
  47.         &finddir($fixtopdir,$topnlink);
  48.         }
  49.         else {
  50.         warn "Can't cd to $topdir: $!¥n";
  51.         }
  52.     }
  53.     else {
  54. #        unless (($dir,$_) = $topdir =~ m#^(.*/)(.*)$#) {
  55. #        ($dir,$_) = ('.', $topdir);
  56.         unless (($dir,$_) = $topdir =~ m#^(.*:)(.*)$#) {
  57.         ($dir,$_) = (':', $topdir);
  58.         }
  59.         $name = $topdir;
  60.         chdir $dir && &wanted;
  61.     }
  62.     chdir $cwd;
  63.     }
  64. }
  65.  
  66. sub finddir {
  67.     local($dir,$nlink) = @_;
  68.     local($dev,$ino,$mode,$subcount);
  69.     local($name);
  70.  
  71.     # Get the list of files in the current directory.
  72.  
  73. #    opendir(DIR,'.') || (warn "Can't open $dir: $!¥n", return);
  74.     opendir(DIR,':') || (warn "Can't open $dir: $!¥n", return);
  75.     local(@filenames) = readdir(DIR);
  76.     closedir(DIR);
  77.  
  78.     if ($nlink == 2 && !$dont_use_nlink) {  # This dir has no subdirectories.
  79.     for (@filenames) {
  80. #        next if $_ eq '.';
  81. #        next if $_ eq '..';
  82. #        $name = "$dir/$_";
  83.         $name = "$dir:$_";
  84.         $nlink = 0;
  85.         &wanted;
  86.     }
  87.     }
  88.     else {                    # This dir has subdirectories.
  89.     $subcount = $nlink - 2;
  90.     for (@filenames) {
  91. #        next if $_ eq '.';
  92. #        next if $_ eq '..';
  93.         $nlink = $prune = 0;
  94. #        $name = "$dir/$_";
  95.         $name = "$dir:$_";
  96.         &wanted;
  97.         if ($subcount > 0 || $dont_use_nlink) {    # Seen all the subdirs?
  98.  
  99.         # Get link count and check for directoriness.
  100.  
  101.         ($dev,$ino,$mode,$nlink) = lstat($_) unless $nlink;
  102.         
  103.         if (-d _) {
  104.  
  105.             # It really is a directory, so do it recursively.
  106.  
  107.             if (!$prune && chdir $_) {
  108.             &finddir($name,$nlink);
  109. #            chdir '..';
  110.             chdir '::';
  111.             }
  112.             --$subcount;
  113.         }
  114.         }
  115.     }
  116.     }
  117. }
  118. 1;
  119.